package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    //客户列表分页（名称模糊查询）
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Customer> customerList= customerDao.list(offSet,rows,customerName);

        map.put("total",customerList.size());
        map.put("rows",customerList);
        return map;
    }

    @Override
    public Boolean saveOrUpdate(Customer customer) {
        //获取客户id
        Integer customerId = customer.getCustomerId();
        try {
            if(customerId==null){
                //新增
                customerDao.save(customer);
                return true;

            }else{
                //修改
                customerDao.update(customer);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    //客户删除（支持批量删除）
    @Override
    public Boolean delete(String ids) {
        try {
            String[] split = ids.split(",");
            customerDao.delete(split);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;

    }
}
